/**
 * 封装axios
 */
import axios from 'axios'

//配置axios请求默认信息
axios.defaults.timeout = 60000;//响应时间
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';//配置请求头
axios.defaults.baseURL = 'http://127.0.0.1:10090';//配置接口地址

//POST传参序列化(添加请求拦截器)
axios.interceptors.request.use((config) => {
    //每次提交的时候默认携带Token
    let token = localStorage.getItem('token')
    if (token != null) {
        config.headers['token'] = token
    }
    return config;
}, (error) => {
    console.log('传参错误')
    return Promise.reject(error);

});

//返回一个Promise(发送post请求)
export function post(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error)
            })
    })

}

//返回一个Promise(发送get请求)
export function get(url, param) {
    return new Promise((resolve, reject) => {
        axios.get(url, { params: param })
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
            .catch((error) => {
                reject(error)
            })
    })

}

export default {
    post,
    get
}
